#include "Calendar.h"
#include "ZStream.h"

const MatchVec Calendar::GetNextTurn() const
{
	TurnGenerator::Couples c;
	m_turnGen.GetTurnNumber(m_currentTurn++, c);

	MatchVec o_matches;
	o_matches.reserve(c.size());

	for(size_t i = 0; i < c.size(); ++i)
	{
		Team* team1 = (*m_teamList)[c[i].first];
		Team* team2 = (*m_teamList)[c[i].second];
		o_matches.push_back(Match(m_sp, team1, team2));
	}

	return o_matches;
}

Calendar::Calendar(ServiceProvider* i_sp)
:m_sp(i_sp)
,m_currentTurn(0)
,m_teamList(0)
{
	assert(i_sp);
}


void Calendar::Clear()
{
	m_turnGen.Clear();
	m_teamList = 0;
	m_currentTurn = 0;
}

void Calendar::Initialize(TeamVec& i_teamList)
{
	m_turnGen.Initialize(i_teamList.size());
	m_teamList = &i_teamList;
	m_currentTurn = 0;
}

const MatchVec Calendar::GetCurrentTurn() const
{
	TurnGenerator::Couples c;
	m_turnGen.GetTurnNumber(m_currentTurn - 1, c);
	
	MatchVec o_matches;
	o_matches.reserve(c.size());

	for(size_t i = 0; i < c.size(); ++i)
	{
		Team* team1 = (*m_teamList)[c[i].first];
		Team* team2 = (*m_teamList)[c[i].second];
		o_matches.push_back(Match(m_sp, team1, team2));
	}

	return o_matches;
}

const size_t Calendar::GetTurnNumber() const
{
	return m_currentTurn;
}

ZStream& Calendar::Serialize(ZStream& o_stream)
{
	o_stream.WriteOpenTag("Calendar");
	o_stream.WriteAttribute("TurnNumber", m_currentTurn);
	m_turnGen.Serialize(o_stream);
	o_stream.WriteCloseTag("Calendar");
	return o_stream;
}

ZStream& Calendar::Deserialize(ZStream& i_stream)
{
	i_stream.SkipLine();
	i_stream.ReadAttribute(m_currentTurn);
	m_turnGen.Deserialize(i_stream);
	i_stream.SkipLine();
	return i_stream;
}
